package cn.wolfcode.wolf2w.redis.service.impl;

import ch.qos.logback.core.util.TimeUtil;
import cn.wolfcode.wolf2w.redis.service.IPersonalService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class PersonalServiceImpl implements IPersonalService {

	@Autowired
	private StringRedisTemplate template;

	@Override
	public Map getViewnumVo(String id,String userId) {
		Map<String,String> map = new HashMap<>();

		String totalKey = RedisKeys.VERIFY_USER_TOTALVIEW.join(id);
		String todayKey = RedisKeys.VERIFY_USER_TODAYVIEW.join(id);

		long time = DateUtil.getDateBetween(DateUtil.getEndDate(new Date()), new Date());	// 有效时间

		template.opsForValue().setIfAbsent(totalKey, "0");
		template.opsForValue().setIfAbsent(todayKey, "0",time, TimeUnit.SECONDS);

		if(!userId.equals(id)){
			// 加一
			template.opsForValue().increment(totalKey);
			template.opsForValue().increment(todayKey);
		}

		map.put("totalView",template.opsForValue().get(totalKey));
		map.put("todayView",template.opsForValue().get(todayKey));

		return map;
	}
}
